﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EnvDTE;
using devtm.Documentation.HierarchyModel;
using Microsoft.RestrictedUsage.CSharp.Compiler;
using Microsoft.RestrictedUsage.CSharp.Semantics;
using CSharpParser.CSharpModel;


namespace CSharpParser.SCharpType
{

    public static class Parser
    {

        /// <summary>
        /// Parses the specified project.
        /// </summary>
        /// <param name="project">The project.</param>
        /// <returns></returns>
        public static IEnumerable<CSharpType> GetTypes(this Project project)
        {

            if ((project != null) && !HierarchyUtilities.IsMiscellaneousProject(project))
            {

                Compilation compilation = CSharpTextSelection.LanguageModel.CreateCompiler(project).GetCompilation();

                IList<CSharpType> oo = compilation.MainAssembly.Types;

                foreach (CSharpType item in oo)
                    yield return item;

            }

        }

        public static CSharpType GetType(this Project project, string @namespace, string name)
        {

            if ((project != null) && !HierarchyUtilities.IsMiscellaneousProject(project))
            {

                Compilation compilation = CSharpTextSelection.LanguageModel.CreateCompiler(project).GetCompilation();

                IList<CSharpType> oo = compilation.MainAssembly.Types;

                foreach (CSharpType item in oo)
                    if (item.Name.Text == name)
                        if (item.FullNamespace() == @namespace)                        
                            return item;
                        
            }

            return null;

        }

        public static bool Match(this CSharpType type, string @namespace, string name)
        {

            if (type.Name.Text == name)
                if (type.FullNamespace() == @namespace)
                {
                    return true;
                }

            return false;
        }

        public static CSharpMember GetMember(this CSharpType type, string name)
        {

            foreach (CSharpMember item in type.Members)
            {
                if (item.Name.Text == name)
                    return item;
            }

            return null;
        }

    }
}
